package com.zhonghui.modlules.user.eshopService.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopDozer.config.IGenerator;
import com.zhonghui.common.eshopEnum.BillDetailEnum;
import com.zhonghui.common.eshopEnum.BillEnum;
import com.zhonghui.common.eshopEnum.BillInfoEnum;
import com.zhonghui.common.eshopEnum.ShopCommonEnum;
import com.zhonghui.modlules.user.eshopDomain.UserBill;
import com.zhonghui.modlules.user.eshopMapper.UserBillMapper;
import com.zhonghui.modlules.user.eshopService.UserBillService;
import com.zhonghui.modlules.user.eshopVo.BillVo;
import com.zhonghui.modlules.user.eshopVo.UserBillQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

@Service
@Transactional(
        propagation = Propagation.SUPPORTS,
        readOnly = true,
        rollbackFor = {Exception.class}
)
public class UserBillServiceImpl extends BaseServiceImpl<UserBillMapper, UserBill> implements UserBillService {
    private UserBillMapper yxUserBillMapper;
    private IGenerator generator;

    public void income(Long uid, String title, String category, String type, double number, double balance, String mark, String linkid) {
        UserBill userBill = UserBill.builder().uid(uid).title(title).category(category).type(type).number( BigDecimal.valueOf(number)).balance(BigDecimal.valueOf(balance)).mark(mark).pm( BillEnum.PM_1.getValue()).linkId(linkid).build();
        this.yxUserBillMapper.insert(userBill);
    }

    public List<BillVo> getUserBillList(int page, int limit, long uid, int type) {
        QueryWrapper<UserBill> wrapper = new QueryWrapper();
        wrapper.lambda().eq(UserBill::getUid,uid).orderByAsc(UserBill::getId);

        wrapper.groupBy("time");
        switch(BillInfoEnum.toType(type)) {
            case PAY_PRODUCT:
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_3.getValue());
                break;
            case RECHAREGE:
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_1.getValue());
                break;
            case BROKERAGE:
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_2.getValue());
                break;
            case EXTRACT:
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_4.getValue());
                break;
            case SIGN_INTEGRAL:
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_2.getValue());
                wrapper.lambda().eq(UserBill::getType, BillDetailEnum.TYPE_10.getValue());
                break;
            default:
                wrapper.lambda().eq(UserBill::getCategory, BillDetailEnum.CATEGORY_1.getValue());
        }

        Page<UserBill> pageModel = new Page((long)page, (long)limit);
        List<BillVo> billDTOList = this.yxUserBillMapper.getBillList(wrapper, pageModel);
        Iterator var9 = billDTOList.iterator();

        while(var9.hasNext()) {
            BillVo billDTO = (BillVo)var9.next();
            LambdaQueryWrapper<UserBill> wrapperT = new LambdaQueryWrapper();
            wrapperT.in(UserBill::getId, Arrays.asList(billDTO.getIds().split(",")));
            billDTO.setList(this.yxUserBillMapper.getUserBillList(wrapperT));
        }

        return billDTOList;
    }


    public List<UserBillQueryVo> userBillList(Long uid, String category, int page, int limit) {
        LambdaQueryWrapper<UserBill> wrapper = new LambdaQueryWrapper();
        wrapper.eq(UserBill::getStatus, ShopCommonEnum.IS_STATUS_1.getValue()).eq(UserBill::getUid, uid).eq(UserBill::getCategory, category).orderByDesc(UserBill::getId);
        Page<UserBill> pageModel = new Page((long)page, (long)limit);
        IPage<UserBill> pageList = this.yxUserBillMapper.selectPage(pageModel, wrapper);
        return this.generator.convert(pageList.getRecords(), UserBillQueryVo.class);
    }


    public void expend(Long uid, String title, String category, String type, double number, double balance, String mark) {
        UserBill userBill = UserBill.builder().uid(uid).title(title).category(category).type(type).number(BigDecimal.valueOf(number)).balance(BigDecimal.valueOf(balance)).mark(mark).pm(BillEnum.PM_0.getValue()).build();
        this.yxUserBillMapper.insert(userBill);
    }

}
